package com.pub.functions;

public class LittleEndianFuncs {
	public static byte[] toByteArray(int iSource, int iArrayLen) {
		byte[] bLocalArr = new byte[iArrayLen];
		for (int i = 0; (i < 4) && (i < iArrayLen); i++) {
			bLocalArr[i] = (byte) (iSource >> 8 * i & 0xFF);
		}
		return bLocalArr;
	}

	public static byte[] toByteArray(long iSource, int iArrayLen) {
		byte[] bLocalArr = new byte[iArrayLen];
		for (int i = 0; (i < 4) && (i < iArrayLen); i++) {
			bLocalArr[i] = (byte) (iSource >> 8 * i & 0xFF);

		}
		return bLocalArr;
	}

	public static int toInt(byte[] bRefArr) {
		int iOutcome = 0;
		byte bLoop;

		if (bRefArr == null) {
			return 0;
		}

		for (int i = 0; i < bRefArr.length; i++) {
			bLoop = bRefArr[i];
			iOutcome += (bLoop & 0xFF) << (8 * i);
		}

		return iOutcome;
	}

	public static int toInt(byte[] bRefArr, int len) {
		int iOutcome = 0;
		byte bLoop;

		if (bRefArr == null || len > bRefArr.length) {
			return 0;
		}

		for (int i = 0, j = 0; i < bRefArr.length && j < len; i++, j++) {
			bLoop = bRefArr[i];
			iOutcome += (bLoop & 0xFF) << (8 * i);
		}

		return iOutcome;
	}

	public static int toInt(byte[] bRefArr, int len, int pos) {
		int iOutcome = 0;
		byte bLoop;

		if (bRefArr == null || pos >= bRefArr.length || (pos + len) > bRefArr.length) {
			return 0;
		}

		for (int i = pos, j = 0; i < bRefArr.length && j < len; i++, j++) {
			bLoop = bRefArr[i];
			iOutcome += (bLoop & 0xFF) << (8 * j);
		}

		return iOutcome;
	}

	public static long toLong(byte[] bRefArr) {
		long iOutcome = 0;
		byte bLoop;

		if (bRefArr == null) {
			return 0;
		}

		for (int i = 0; i < bRefArr.length; i++) {
			bLoop = bRefArr[i];
			iOutcome += (bLoop & 0xFF) << (8 * i);

		}

		return iOutcome;
	}

	public static long toLong(byte[] bRefArr, int len, int pos) {
		long iOutcome = 0;
		byte bLoop;

		if (bRefArr == null || pos >= bRefArr.length || (pos + len) > bRefArr.length) {
			return 0;
		}

		for (int i = pos, j = 0; i < bRefArr.length && j < len; i++, j++) {
			bLoop = bRefArr[i];
			iOutcome += (bLoop & 0xFF) << (8 * j);
		}

		return iOutcome;
	}
}
